<?php

namespace app\backstage\controller;

use think\Request;
use app\backstage\model\Admin as AdminModel;
use app\backstage\model\Role;
use Db;
// use think\facade\Request;

class Admin extends BaseAuth
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request,AdminModel $admin)
    {
        if($request->isAjax()){
        
            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

            $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['a.create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['a.create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['a.name|a.phone|a.email','like',"%{$params['seach_text']}%"];
            }
            
            //权限列表
            $data = $admin->alias('a')->join('rbac_role b','a.role_id=b.id','left')
                        ->field('a.id,a.name,a.phone,a.email,a.status,a.login_time,a.login_ip,a.comment,a.create_time,b.role_name')
                        ->where($where)->order('a.id','desc')
                        ->limit($offset,$pageSize)->select();
            
            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                    if(empty($v['login_time'])){
                        $v['login_time'] = '未登录';
                    }else{
                        $v['login_time'] = date('Y-m-d H:i',$v['login_time']);
                    }
                }
            }


            if(!empty($where)){
                $count = count($data);
            }else{
                $count = $admin->count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'adminData'=>$data,'pageData'=>$pageData];
        
        }

   
        return view('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
        $roleList = Role::field('id,role_name')->select();
        return view('create',['role_list'=>$roleList]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){
            
            $data = $request->post();

            $admin = new AdminModel($data);

            //验证参数 
            $check = $admin->valiData($data);
            
            if($check['code'] !=200) return $check;

            $admin->password = md5(md5($data['password']).config('database.password_salt'));
            //插入数据
            $admin->create_time = time();
            $res = $admin->allowField(true)->save();
            
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'新增失败'];
            }

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $info = AdminModel::where('id',$id)->find();
        $roleList = Role::field('id,role_name')->select();


        return view('edit',['role_list'=>$roleList,'info'=>$info]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){
            
            $data = $request->post();

            $admin = new AdminModel();

            //验证参数 
            $check = $admin->valiData($data);

            if($check['code'] !=200) return $check;

            if($data['password']){
                $data['password'] = md5(md5($data['password']).config('database.password_salt'));
            }else{
                unset($data['password']);
            }

            //修改数据
            $res = $admin->allowField(true)->save($data,['id'=>$id]);
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'修改失败'];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id,$is_sure=2)
    {
        if(in_array(1,$id)){
            return ['code'=>101,'msg'=>'系统管理员不能删除'];
        }

        if($is_sure==1){
//            dump($id);die;


            $res = Db::name('rbac_admin')->delete($id);
        }else{
            $res = AdminModel::destroy($id);

        }

        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }

     /**
     * 批量删除
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function delete_all()
    {   

        if($ids = request()->post('ids')){

            if($sure = request()->get('is_sure')){

                $res = Db::table('admin')->delete($ids);
//                $res = AdminModel::destroy($ids,true);
            }else{
                $res = AdminModel::destroy($ids);

            }

            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'删除失败'];
            }
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
      
    }
    
      /**
     *修改状态
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function change_status(Request $request)
    {   

       if($request->isAjax()){
           
          $data = $request->param();

          if(!empty($data['id']) && !empty($data['status'])){

             $admin = new AdminModel();
             $res = $admin->save(['status'=>$data['status']],['id'=>$data['id']]);

             if (!$res)return ['code'=>101,'msg'=>'删除失败'];
             return ['code'=>200];
          }else{
              return ['code'=>101,'msg'=>'删除失败'];
          }
       }
      
    }

    /**
     *回收站
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function recycle(Request $request,AdminModel $admin)
    {

        if($request->isAjax()){

            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

            $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['a.create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['a.create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['a.name|a.phone|a.email','like',"%{$params['seach_text']}%"];
            }
            //权限列表
            $data = $admin ->onlyTrashed()->alias('a')->join('rbac_role b','a.role_id=b.id','left')

                ->field('a.id,a.name,a.phone,a.email,a.status,a.delete_time,a.login_ip,a.comment,a.create_time,b.role_name')
                ->where($where)->order('a.id','desc')
                ->limit($offset,$pageSize)->select();

            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                    $v['delete_time'] = date('Y-m-d H:i',$v['delete_time']);

                }
            }

            if(!empty($where)){
                $count = count($data);
            }else{
                $count = $admin->onlyTrashed()->count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'adminData'=>$data,'pageData'=>$pageData];
        }
        return view('recycle');
    }


    //还原
    public  function retore(Request $request,AdminModel $admin){
        if($request->isAjax()){
            $ids = $request->param('ids');

            if($ids){
                $admin = $admin->onlyTrashed()->find($ids);
                $res = $admin->restore();

                if($res){
                    return ['code'=>200];
                }else{
                    return ['code'=>100,'msg'=>'无法还原'];
                }
            }
        }
    }
    
}
